#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 23:38:49 2025

@author: jack
"""
from random import randint



# 骰子对象
class Die:

    def __init__(self, num_sides=6):
        self.num_sides = num_sides

    def roll(self):
        return randint(1, self.num_sides)
    


# 骰子每次的值

die_1 = Die()

throw_times = 1000

results = []

for roll_num in range(throw_times):
    result = die_1.roll()
    results.append(result)

results



# 6个面出现的频率

freqs = []

for value in range(1, die_1.num_sides+1):

    # 统计results,1-6出现的次数
    freq = results.count(value)
    freqs.append(freq)

freqs


# 绘制交互图


from plotly.graph_objs import Bar, Layout
from plotly import offline


# plotly不能接受range,需转为list
x_values = list(range(1, die_1.num_sides+1))

# 使用Bar类,data可能包含多个元素,此处必须是list
data = [Bar(x=x_values, y=freqs)]

x_axis_config = {'title': '结果'}
y_axis_config = {'title': '结果的频率'}

# 使用Layout类
my_layout = Layout(title=f'掷一个D6 {throw_times}次的结果', xaxis=x_axis_config, yaxis=y_axis_config)
offline.plot({'data': data, 'layout': my_layout}, filename='d6.html')

